#ifndef __AWTRIXController_H
#define __AWTRIXController_H

/**********************************************************************更新日志***************************************************************************
 *  2023-9-5   更新和风天气功能，b站粉丝数目，使用ArduinoJson V6版本
 *  2023-9-26  更新按键1长短按功能，长按设置亮度，短按功能切换
 *  2023-12-24 更新天气、b站获取失败显示提示
 * *******************************************************************************************************************************************************/
#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClient.h>
#include <Adafruit_GFX.h>
#include <FastLED.h>
#include <FastLED_NeoMatrix.h>
#include <Fonts/TomThumb.h>

#include <LightDependentResistor.h>
#include <Wire.h>
#include "SoftwareSerial.h"
#include "RTClib.h"
#include "DHT.h"
#include <WiFiManager.h>
#include <NtpClientLib.h>
#include <TimeLib.h>
#include <SimpleTimer.h>

#include "HTTPFunction/HeFeng.h"
// #include "WeatherICON/WeatherICON.h"

/******************************************* 哔哩哔哩参数定义 ********************************************/
#define  BILIBILI_UID       "104411760"                               //B站用户UID
#define  WEATHER_APIKEY     "d00c44fefbbd427895640d391911feef"        //和风天气秘钥
#define  WEATHER_CITY       "101020800"                               //天气 上海青浦
//#define  WEATHER_CITY     "auto_ip";                                //自动IP定位
#define Updata_Weather_SECS        10 * 60       // 10分钟更新一次天气
#define Updata_Bilibili_SECS       60 * 59       // 30分钟更新一次粉丝数
#define Updata_Errors_Times        3             //更新错误重试次数

HeFengCurrentData currentWeather; //实例化对象
HeFengForeData foreWeather[3];
HeFeng HeFengClient;
class WEATHER_ICON {
    public:
        void Show_SunnyDay();
        void Show_CloudyDay();
        void Show_CloudyNightDay();
        void Show_cloudyDAY();
        void Show_Sandstorm();
        void Show_Rain();
        void Show_Sleet();
        void Show_Snowflake();
        void Show_Error_Weather();
        void Show_Thunderstorm();
        void Show_Wind_Day();
        void Show_Fog_Day();
        void Show_Moderate_Snow();
        void Show_Snow_Shower();
        void Show_Haze();
        void Show_Moderate_Rain();
        void Show_Thundershower();
};
WEATHER_ICON IconClient;
int meteoconIcon = 0;
long timeSinceLastWeatherUpdate = 0;            //上次更新后的时间
long timeSinceLastBilibiliUpdate = 0;           //上次天气更新后的时间

void Show_Temperature(int Temperature);
void Show_Humidity(int Humidity);                
bool Show_BiLiBiLi_Information(uint32_t Fans_Number); 
void BILIBIL_ICON();  
bool Show_Room_Temperature_Humidity(int Temperature,int Humidity);   
bool Show_CurrentDay_Weather(int weather,int Temperature,int Humidity);                         
/*********************************************************************************************************/
/******************************************* 按键参数定义 ************************************************/
#define BUTTON1_PIN 12                //将按钮引脚绑定到GPIO 12
#define BUTTON2_PIN 2                 //将按钮引脚绑定到GPIO 2
#define BUTTON1_Long_Time_Threshold   2000
uint8_t KEY1_Count = 6; 
bool Change_Mode_Flag = false;
long Button1firstTouchTime = 0,Button2firstTouchTime = 0;
uint8_t Key1LongPressFlag = 0 ,Key2LongPressFlag = 0 ;

ICACHE_RAM_ATTR void touch1DownInterrupt();
ICACHE_RAM_ATTR void touch2DownInterrupt();
ICACHE_RAM_ATTR void touch1UPInterrupt();
ICACHE_RAM_ATTR void touch2UPInterrupt();
/*********************************************************************************************************/
/******************************************* Matrix像素参数定义 ******************************************/
#define AwtrixMode              7             //像素时钟功能模式
#define MATRIX_Hight            8             //像素屏y轴数量
#define AwtrixBrightnessMax     120           //像素时钟最大亮度
#define AwtrixBrightnessMin     10            //像素时钟最小亮度

#ifdef MATRIX_PRO                             //表示用的32*8的像素屏幕
#define LED_Number              256           //像素屏LED数量
#define MATRIX_Weight           32            //像素屏x轴数量
#else
#define LED_Number              192           //像素屏LED数量
#define MATRIX_Weight           24            //像素屏x轴数量
#endif

// Matrix Settings                              
CRGB leds[LED_Number];
FastLED_NeoMatrix *matrix;
uint8_t brightness = AwtrixBrightnessMin;  

typedef struct LED_Color_Date{                //drawFastHLine//画水平线
  uint32_t h;                                 //drawFastVLine//画垂直线
  uint32_t s;                                 //fillRect//画矩形
  uint32_t v;                                 //drawCircle//画空心圆                     
  uint8_t R_Color;
  uint8_t G_Color;
  uint8_t B_Color;
}LED_Color_Date;

void Generate_LED_Color(LED_Color_Date* data);
void matrixInit();
bool Show_Level_Running_Water(uint8_t x,uint8_t y,uint8_t End_x,uint8_t R_Color_t,uint8_t G_Color_t,uint8_t B_Color_t,uint16_t Add_Time);
bool Show_Brush_Animation(uint8_t kind);
static void led_HSV_To_RGB(LED_Color_Date* data);
/********************************************************************************************************/
/******************************************* NTP参数定义 ************************************************/
String lnyear = "2023";
String lnnmonth = "";
String lnnday = "";
String huor = "";
String Minute_t = "";
String seconds = "";
int WeakDAY = 0;
String Lasthuor = "",CurrentHour="";
String LastMinute_t = "",CurrentMinute_t="";
int8_t timeZone = 8;
const PROGMEM char *ntpServer = "ntp1.aliyun.com";
static uint8_t RunMinute = 0 , RunSeconds = 0 ;

typedef enum Timepiece{
  Pause = 0,
  Start ,
  Clear
}Timepiece;

Timepiece Timepiece_t;
void date_time();
bool Clock_Mode();
bool Show_Time(uint8_t R_Color_t,uint8_t G_Color_t,uint8_t B_Color_t,uint32_t time);
bool ShoW_Date(uint8_t x,uint8_t y,uint8_t end_x,uint32_t Dalay_Time);  //显示日期
bool ShoW_Year(uint8_t x,uint8_t y,uint8_t end_x,uint32_t Dalay_Time);
bool DesktopCal_Mode();
/**********************************************************************************************************/
/******************************************* DHT11温湿度参数定义 ******************************************/
#define DHTPIN 5                // DHT ii使用的PIN脚
#define DHTTYPE DHT11           // DHT 11
DHT dht(DHTPIN, DHTTYPE);
typedef struct RoomCurrentData {
  float tmp = 24.0;
  float hum = 50.0;
}RoomCurrentData;
RoomCurrentData RoomCurrentData_t;
void Get_Room_Temperature_Humidity(RoomCurrentData* date);
/*************************************************************************************************************/
uint8_t Change_Awtrix_Brightness();
LightDependentResistor myldr(A0,1000,LightDependentResistor::GL5528);
SoftwareSerial mySoftwareSerial(D7, D5); // RX, TX
SimpleTimer timer;
bool Random_First_Flag = 0;

void Show_Timepiece_Fuction();
void Timepiece_ICON();
void Show_SET_Brightness(uint8_t value);
void Light_ICON();

// boolean WiFi_Connected(String ssid, String passsprase, int timeout);
void Work_Mode(uint8_t mode_t);
void Show_Wifi_IP_Adresss();
void Simple_timer_1();
void hardwareAnimatedSearch(int typ, int x, int y);
void hardwareAnimatedCheck(int typ, int x, int y);
bool Random_Mode();
// void Fuction_Menu(uint8_t n);
//MERCURY_1A4E= 路由器ssid  12345678= 路由器密码。请改成你自己的ssid 密码
// if (WiFi_Connected("ChinaNet-Nw6Y", "GAsh123456", 20000)) {
// if (WiFi_Connected("MERCURY_1A4E", "123456789", 20000)) {
// 	Serial.println("WiFi Connected Success");
// } else {
//   Serial.println("WiFi Connected fail");
// }

#endif // __AWTRIXController_H

// /**********************************************************************************************************************
// * 功能说明；  显示功能菜单
// * 输入参数：  无
// * 返回值：    无
// **********************************************************************************************************************/
// void Fuction_Menu(uint8_t n){
//   LED_Color_Date data;

//   Generate_LED_Color(&data);
//   switch (n){
//     case 0:
//       matrix->clear();
//       matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//       matrix->setCursor(0,6);
//       matrix->print("Colock");
//       matrix->show();
//       break; 
//     case 1:
//       for (int x = 24; x >= -37 ;x--){
//           matrix->clear();
//           matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//           matrix->setCursor(0,6);
//           matrix->print("Calendar");
//           matrix->show();
//           delay(250);
//         }
//       break; 
//     case 2:
//       for (int x = 24; x >= -70 ;x--){
//         matrix->clear();
//         matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//         matrix->setCursor(x,6);
//         matrix->print("Room Environment");
//         matrix->show();
//         delay(250);
//       }
//       break;
//     case 3:
//       matrix->clear();
//       matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//       matrix->setCursor(0,6);
//       matrix->print("BILIBILI");
//       matrix->show();
//       break;
//     case 4:
//       matrix->clear();
//       matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//       matrix->setCursor(2,6);
//       matrix->print("Weather");
//       matrix->show();
//     case 5:
//       matrix->clear();
//       matrix->setTextColor(matrix->Color(data.R_Color,data.G_Color,data.B_Color));
//       matrix->setCursor(0,6);
//       matrix->print("Timepiece");
//       matrix->show();
//     default:
//       break;
//   }
// }